package io.renren.wh.interfaces.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.wh.interfaces.dao.InterfacesDao;
import io.renren.wh.interfaces.entity.InterfacesEntity;
import io.renren.wh.interfaces.service.InterfacesService;
import io.renren.wh.merchant.dao.InfoDao;
import io.renren.wh.merchant.entity.RateEntity;
import io.renren.wh.merchant.service.RateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("interfacesService")
public class InterfacesServiceImpl extends ServiceImpl<InterfacesDao, InterfacesEntity> implements InterfacesService {

    @Autowired
    InfoDao infoDao;

    @Autowired
    RateService rateService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<InterfacesEntity> queryWrapper = new QueryWrapper<InterfacesEntity>();

        if (params != null) {
            if (!StringUtils.isEmpty(params.get("interfacecode"))) {
                queryWrapper.eq("interfacecode",params.get("interfacecode"));
            }
            if (!StringUtils.isEmpty(params.get("interfacename"))) {
                queryWrapper.eq("interfaceName",params.get("interfacename"));
            }
        }
        IPage<InterfacesEntity> page = this.page(
                new Query<InterfacesEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<InterfacesEntity> faceList(Integer id) {
        List<InterfacesEntity> list = this.list(null);

        List<InterfacesEntity> collect=null;
        if(list!=null){
        collect = list.stream().map((item) -> {
            RateEntity rateEntity = SelectMerchaninfoInterfacesRelation(id, item.getId());
            if(rateEntity!=null) {
                if (item.getId().equals(rateEntity.getInterfaceid())) {
                    item.setCheck(0);
                }else{
                    item.setCheck(1);
                }
            }
            return item;
        }).collect(Collectors.toList());

        }
        return collect;


    }

    @Override
    public InterfacesEntity selectByPayType(Integer payType) {

        InterfacesEntity interfacesEntity = this.baseMapper.selectOne(new QueryWrapper<InterfacesEntity>().eq("interfacecode", payType));

        return interfacesEntity;
    }

    /**
     * 商户费率
     * @param infoId
     * @param faceId
     * @return
     */
    private RateEntity SelectMerchaninfoInterfacesRelation(Integer infoId, Integer faceId){
        RateEntity rateEntity = infoDao.SelectMerchaninfoInterfacesRelation(infoId, faceId);


        return rateEntity;
    }

}
